<?php
/**
 * Created by PhpStorm.
 * User: Edward
 * Date: 2019/5/30
 * Time: 14:16
 */

namespace app\admin;

use app\admin\model\AdminToken;
use app\lib\exception\NotFoundException;
use app\lib\exception\TokenException;
use app\Validate\AdminApiValidate;
use think\Controller;

/**
 * 入口方法
 * Class BaseController
 * @package app\admin
 */
class BaseController extends Controller {
    protected $admin = null;  //管理员信息
    protected $shop  = null;  //商家信息

    public function initialize()
    {
        define("TestPrice",2); //1是测试价格 2是正式价格
        define("UID",1);
        $listType = [
            1=>"入库单",
            2=>"补货单",
            3=>"出库单",
            4=>"调批单",
            5=>"退货单",
            6=>"其他"
        ];
        define("ListType",$listType);

        //登录权限判断
        (new AdminApiValidate())->goCheck();
        $post = \think\facade\Request::request();
        $where = [
            ['token','eq',$post['token']],
            ['admin_id','eq',$post['admin_id']],
            ['overDay_time','gt',time()]
        ];
        $count = (new AdminToken())->getCount($where);
        if($count==0){
            throw new TokenException(['msg'=>'token失效']);
        }
        $admin = (new \app\admin\model\Admin())->findOne([['id','eq',$post['admin_id']],['status','eq',1]]);
        if(!$admin){
            throw new NotFoundException(['msg'=>'当前登录账号不存在或被封停']);
        }

        //获取管理员信息
        $admin = $admin->toArray();
        $this->admin = $admin;

        //如果是商家则赋值商家信息
        if($admin["uid"] > 0){
            $shop = (new \app\admin\model\Shop())->findOne([['id','eq',$admin["uid"]]]);
            if($shop == null){
                throw new NotFoundException(['msg'=>'当前账号未绑定商家']);
            }
            $this->shop = $shop->toArray();
        }
    }
}

// 备用方法
//   $server = \think\facade\Request::server();
//   print_r($server['REDIRECT_URL']);//当前访问域名
//        if($admin['rank']>1){ //如果不是总账号则需要判断是否有权限
//            $where_rule = [
//                ['account_type','eq',$admin['account_type']],
//                ['type','eq',3],
//            ];
//            $list = (new AuthRule())->where($where_rule)->cache(true,120)->column("rule_val");
//            $auth_rule = (new AuthGroupRule())->where([['role_id','eq',$admin['role_id']]])->column("rule_id");
//            $where_my_rule = [
//                ['id','in',$auth_rule],
//                ['type','eq',3],
//            ];
//            $myRule = (new  AuthRule())->where($where_my_rule)->column("rule_val");
////            if(!isset($server['REQUEST_URI'])){
////                throw new FailedException(['msg'=>$server]);
////            }
//            if(in_array($server['REQUEST_URI'],$list)&&!in_array($server['REQUEST_URI'],$myRule)){
//                throw  new NoJurisdiction();
//            }
//        }